package com.mc.jf.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {
    static SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyy-MM-dd");

    public static String dateToStr(Date date) {
        return ymdFormat.format(date);
    }
    public static Date strToDate(String source) {
        Date date = null;
        try {
            date = ymdFormat.parse(source);
        } catch (Exception e) {
        }

        return date;
    }


    public static Date addMonth(Date date, int month) {

        if (date == null) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();//得到Calendar实例
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        Date starDate = calendar.getTime();//得到时间赋给Data

        return starDate;
    }

    public static List<String> getAllDateOfYear() {

        Date date = DateUtils.strToDate("2023-05-17");
        List<String> list = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        int year = cal.get(Calendar.YEAR);
        while(cal.get(Calendar.YEAR) == year){
            list.add(DateUtils.dateToStr(cal.getTime()));
            cal.add(Calendar.DATE, 1);
        }
        return list;
    }
}
